package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateUpdateInfoDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateInfoDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 处理属性模版数据的控制器类
 * @Author: sweeterjava@163.com
 * @Date: 2022/10/26
 * @Time: 15:41
 */

@Api(tags = "04.属性模版管理")
@Slf4j
@RestController
@RequestMapping("/attribute-templates")
public class AttributeTemplateController {

    @Autowired
    public IAttributeTemplateService attributeTemplateService;

    public AttributeTemplateController() {
        log.debug("创建控制器对象：AttributeTemplateController");
    }

    @ApiOperation("添加新属性模版")
    @ApiOperationSupport(order = 101)
    @PostMapping("/add-new")
    public String addNew(AttributeTemplateAddNewDTO attributeTemplateAddNewDTO) {
        log.debug("开始处理添加属性模版请求,参数：{}", attributeTemplateAddNewDTO);
        try {
            attributeTemplateService.addNew(attributeTemplateAddNewDTO);
            log.debug("添加属性模版成功");
            return "添加属性模版成功";
        } catch (ServiceException e) {
            String message = e.getMessage();
            log.debug("添加属性模版失败！属性模版名称已经被占用！");
            return message;
        } catch (RuntimeException e) {
            String message = e.getMessage();
            log.debug("添加数据失败！程序运行过程中出现了RuntimeException！");
            return message;
        } catch (Throwable e) {
            String message = e.getMessage();
            log.debug("添加数据失败！程序运行过程中出现了Throwable！");
            return message;
        }

    }

    @ApiOperation("根据id删除属性模版")
    @ApiOperationSupport(order = 201)
    @PostMapping("/{id:[0-9]+}/delete")
    public String delete(@PathVariable Long id){
        String message = "尝试删除id为" + id + "的属性模版";
        log.debug(message);
        return message;
    }

    @ApiOperation("【已过期】根据name删除属性模版")
    @ApiOperationSupport(order = 901)
    @PostMapping("/{id:[a-z]+}/delete2")
    public String delete2(@PathVariable String name){
        String message = "尝试删除名为" + name + "的属性模版";
        log.debug(message);
        return message;
    }

    @ApiOperation("【已过期】删除属性模版")
    @ApiOperationSupport(order = 902)
    @PostMapping("/test/delete3")
    public String delete3(){
        String message = "尝试删除所有属性模版";
        log.debug(message);
        return message;
    }

    // http://localhost:9080/attribute-templates/9527/info/update
    @ApiOperation("修改属性模板基本资料")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "属性模板id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/info/update")
    public JsonResult<Void> updateInfoById(@PathVariable Long id, AttributeTemplateUpdateInfoDTO attributeTemplateUpdateInfoDTO) {
        log.debug("开始处理【修改属性模板基本资料】的请求，id={}，新基本资料={}", attributeTemplateUpdateInfoDTO);
        attributeTemplateService.updateInfoById(id, attributeTemplateUpdateInfoDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/attribute-templates/9527
    @ApiOperation("根据id查询属性模板详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "属性模板id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<AttributeTemplateStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询属性模板详情】的请求，参数：{}", id);
        AttributeTemplateStandardVO attributeTemplate = attributeTemplateService.getStandardById(id);
        return JsonResult.ok(attributeTemplate);
    }
}
